package cn.kgc.tangcco.service.impl;

import cn.kgc.tangcco.dao.ClazzDao;
import cn.kgc.tangcco.model.ResponseText;
import cn.kgc.tangcco.model.ResultCode;
import cn.kgc.tangcco.pojo.Clazz;
import cn.kgc.tangcco.service.ClazzService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author 李昊哲
 * @Description
 * @create 2021/1/12 9:37
 */
@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzDao clazzDao;

    @Override
    public ResponseText<List<Clazz>> queryClazzs(Clazz clazz) {
        List<Clazz> clazzList = clazzDao.findAll();
        if (clazzList == null || clazzList.size() == 0) {
            return new ResponseText<>(ResultCode.DATA_EMPTY.getCode(), ResultCode.DATA_EMPTY.getMsg(), new ArrayList<>());
        }
        return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), clazzList);
    }

    @Override
    public ResponseText<Clazz> queryClazz(Integer id) {
        if (id == null) {
            return new ResponseText<>(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        }
        Optional<Clazz> optional = clazzDao.findById(id);
        if (optional.isEmpty()) {
            return new ResponseText<>(ResultCode.DATA_EMPTY.getCode(), ResultCode.DATA_EMPTY.getMsg(), null);
        }
        return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), optional.get());
    }

    @Override
    @Transactional(readOnly = true)//查询的只读就行
    public ResponseText<Clazz> getClazz(Integer id) {
        if (id == null) {
            return new ResponseText<>(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        }
        try {
            Clazz clazz = clazzDao.getOne(id);
            if (clazz != null && !StringUtils.isBlank(clazz.getName())) {
                return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), clazz);
            }
            return new ResponseText<>(ResultCode.DATA_EMPTY.getCode(), ResultCode.DATA_EMPTY.getMsg(), null);
        } catch (Exception exception) {
            return new ResponseText<>(ResultCode.DATA_EMPTY.getCode(), ResultCode.DATA_EMPTY.getMsg(), null);
        }
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class}, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false)
    @Override
    public ResponseText saveOrUpdate(Clazz clazz) {
        if (clazz == null || StringUtils.isEmpty(clazz.getName())) {
            return new ResponseText<>(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), ResultCode.FAILED.getDesc());
        }
        if (clazz != null && clazz.getId() != null && clazz.getId() > 0) {
            Optional<Clazz> optional = clazzDao.findById(clazz.getId());
            if (optional.isEmpty()) {
                return new ResponseText<>(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
            } else {
                clazz.setId(optional.get().getId());
            }
        }
        clazz = clazzDao.saveAndFlush(clazz);
        if (clazz == null) {
            return new ResponseText<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), ResultCode.FAILED.getDesc());
        }
        return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), clazz);
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class}, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false)
    @Override
    public ResponseText remove(Integer id) {
        if (id == null) {
            return new ResponseText<>(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), ResultCode.PARAM_ERROR.getDesc());
        }
        clazzDao.deleteById(id);
        return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), ResultCode.SUCCESS.getDesc());
    }

    @Override
    public ResponseText<Long> count() {
        long count = clazzDao.count();
        if (count > 0) {
            return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), count);
        }
        return new ResponseText<>(ResultCode.DATA_EMPTY.getCode(), ResultCode.DATA_EMPTY.getMsg(), 0L);
    }

    @Override
    public ResponseText<Boolean> exists(Integer id) {
        if (id == null) {
            return new ResponseText<>(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), false);
        }
        return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), clazzDao.existsById(id));
    }
}
